package cn.cdqf.manager.service.impl;

import cn.cdqf.coupon.common.CommonCanstant;
import cn.cdqf.coupon.util.FutureUtils;
import cn.cdqf.coupon.util.IdUtils;
import cn.cdqf.global.kafka.KafkaSender;
import cn.cdqf.global.util.RedisUtils;
import cn.cdqf.manager.dao.CCouponCodeUserMapper;
import cn.cdqf.manager.dao.CCouponTemplateMapper;
import cn.cdqf.manager.entity.CCouponTemplate;
import cn.cdqf.manager.param.CouponTemplateParam;
import cn.cdqf.manager.service.CouponTemplateService;
import cn.cdqf.manager.util.CouponCodeUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@SuppressWarnings("all")
public class CouponTemplateServiceImpl implements CouponTemplateService {

   @Autowired
   CCouponTemplateMapper cCouponTemplateMapper;

   @Autowired
   private IdUtils idUtils;

   @Autowired
   private CouponCodeUtil couponCodeUtil;

   /*@Autowired
   private CCouponCodeUserMapper cCouponCodeUserMapper;*/

   @Autowired
   private RedisUtils redisUtils;

   @Autowired
   private KafkaSender kafkaSender;

   @Autowired
   private ObjectMapper objectMapper;

    @Override
    @Transactional
    public void insert(CouponTemplateParam couponTemplateParam) {

        //todo logo,linkurl cid
        //String才是bigdecimal的最高精度
        CCouponTemplate build = CCouponTemplate.builder().cAmount(new BigDecimal(String.valueOf(couponTemplateParam.getcAmount())))
                .cAmountLimit(new BigDecimal(String.valueOf(couponTemplateParam.getcAmountLimit())))
                .cAreaId(couponTemplateParam.getcAreaId())
                .cCategory(couponTemplateParam.getcCategory())
                .cConsumeStatus(couponTemplateParam.getcConsumeStatus())
                .cExpireDay(couponTemplateParam.getcExpireDay())
                .cExpireType(couponTemplateParam.getcExpireType())
                .cLimiter(couponTemplateParam.getcLimiter())
                //.cLinkUrl(couponTemplateParam.getCMerchantId())
                .cMerchantName(couponTemplateParam.getcMerchantName())
                .cMerchantName(couponTemplateParam.getcMerchantName())
                .cProductLine(couponTemplateParam.getcProductLine())
                .cMerchantId(couponTemplateParam.getcMerchantId())
                .cDesc(couponTemplateParam.getcDesc())
                .cCount(couponTemplateParam.getcCount())
                .build();
        //如果要分库分表,生成模板id
        String couponId = FutureUtils.get(idUtils.getId(couponTemplateParam.getcMerchantId()));
        build.setcId(couponId);
        //插入模板表
        cCouponTemplateMapper.insertSelective(build);

        //优惠券码
        int count = couponTemplateParam.getcCount();
        List<String> list = couponCodeUtil.generate(count, couponTemplateParam.getcMerchantId());

        /*放入redis 模板id,所有的优惠券码,但是如果是微服务项目,只能自己操作自己的库
        redisUtils.sset(CommonCanstant.CouponConstant.REDIS_COUPON_COUPONID+couponId,list.toArray());
        //将优惠券码插入中间表
        cCouponCodeUserMapper.batchInsert(list,couponTemplateParam.getcMerchantId(),couponId);*/

        //kafka消息消费,httpclient远程调用都可以做,发送kafka到其他微服务,进行数据库的插入
        //参数:优惠券码,模板id,商家id <商家id,<模板id,list<优惠券码>>>
        HashMap<String, Map<String,List<String>>> map = new HashMap<>();
        HashMap<String, List<String>> map1 = new HashMap<>();
        map1.put(couponId,list);
        map.put(couponTemplateParam.getcMerchantId(),map1);
        try {
            kafkaSender.sendMessage(CommonCanstant.KafkaKey.COUPONID_MERCHANTID_TO_CODE,objectMapper.writeValueAsString(map));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }


        //优惠金额
        double amount = couponTemplateParam.getcAmount();
        //根据价格等级缓存到redis
        if(amount>=1||amount<=50){
            //商户id+level对应模板一个id
            redisUtils.sset(CommonCanstant.CouponConstant.COMMON_COUPON_MERCHANTID_LEVEL + CommonCanstant.SPLIT + couponTemplateParam.getcMerchantId() + CommonCanstant.SPLIT + 1,couponId);
        }
    }
}
